package com.sl.au.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.UnexpectedRollbackException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.Account;
import com.sl.au.entity.EXDELoginInfo;
import com.sl.au.entity.Goods;
import com.sl.au.entity.Role;
import com.sl.au.entity.User;
import com.sl.au.repository.AccountRepository;
import com.sl.au.repository.EXDELoginInfoRepository;
import com.sl.au.repository.GoodsRepository;
import com.sl.au.repository.UserRepository;
import com.sl.au.service.AccountService;
import com.sl.au.service.GoodsService;
import com.sl.au.service.ModuleService;

@Service("goodsService")
@Transactional
public class GoodsServiceImpl extends BaseServiceImpl implements GoodsService {

	@Autowired
	GoodsRepository goodsRepository;
	
	@Override
	public GoodsRepository getRepository() {
		return this.goodsRepository;
	}
	
	@Override
	public List<Goods> getPage(int start, int length, String key, String orderName, Direction dir){
		Page<Goods> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		if (StringUtils.hasText(key)) {
			pages = goodsRepository.getByKey(key, pageable);
		} else {
			pages = goodsRepository.findAll(pageable);
		}

		return pages.getContent();
	}

	@Override
	public int getPageCount(String key) {
		int count = 0;
		if (StringUtils.hasText(key)) {
			count = goodsRepository.getCountByKey(key);
		} else {
			count = goodsRepository.getCount();
		}
		return 0;
	}

	@Override
	public List<Goods> getPageByDate(int start, int length, Date date, String orderName, Direction dir) {
		Page<Goods> pages = null;
		Pageable pageable = new PageRequest(start / length, length, dir, orderName);
		pages = goodsRepository.getGoodsByDate(date, pageable);
		return pages.getContent();
	}




}
